using System;
using System.Collections.Generic;
using UnityEngine;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class Head : MonoBehaviour
{
	[SerializeField]
	private Rigidbody[] _rigs;

	private Progress progress;

	public bool isOpponent;

	private GameplayAudio gameplayAudio;

	private GameplayHaptic gameplayHaptic;

	private float rotationSpeed;

	public int amount;

	[SerializeField]
	private Item servoUpgrade;

	private MapCreatorEditor _mapGenerator;

	public int _addedDifficulty;

	public int _randomRoll = 31;

	private int servoLvl;

	private int lvl;

	public List<Collider> _colliders = new List<Collider>();

	private void Start()
	{
		SetKinematic(active: true);
		progress = UnityEngine.Object.FindObjectOfType<Progress>();
		UnityEngine.Object.FindObjectOfType<TapControl>()._headPos = base.transform;
		gameplayAudio = GameObject.Find("GameplaySounds").GetComponent<GameplayAudio>();
		gameplayHaptic = GameObject.Find("Manager").GetComponent<GameplayHaptic>();
		Invoke("UpdateRotationSpeed", 0.1f);
		servoLvl = InventoryManager.Instance.GetInventoryCount(servoUpgrade);
		lvl = SingletonMB<GameManager>.Instance.Currentlevel;
		InventoryManager instance = InventoryManager.Instance;
		instance.OnItemPurchased = (Action)Delegate.Combine(instance.OnItemPurchased, new Action(UpdateDifficulty));
		GameManager instance2 = SingletonMB<GameManager>.Instance;
		instance2.OnGameStart = (Action)Delegate.Combine(instance2.OnGameStart, new Action(UpdateDifficulty));
		if (_mapGenerator == null)
		{
			_mapGenerator = UnityEngine.Object.FindObjectOfType<MapCreatorEditor>();
		}
	}

	private void OnDisable()
	{
	}

	private void UpdateDifficulty()
	{
		lvl = SingletonMB<GameManager>.Instance.Currentlevel;
		if (_mapGenerator == null)
		{
			_mapGenerator = UnityEngine.Object.FindObjectOfType<MapCreatorEditor>();
		}
		int num = 31 - amount;
		float difficultyMultiplier = _mapGenerator.CurrentLevel.difficultyMultiplier;
		Debug.Log("Level Difficulty Modifier: " + difficultyMultiplier);
		num = Mathf.CeilToInt((float)num * difficultyMultiplier);
		Debug.Log("Level Difficulty : " + num);
		_randomRoll = Mathf.Max(1, num);
	}

	public static void AddPhysics(Collider other)
	{
		if (!other.gameObject.TryGetComponent<Rigidbody>(out var component))
		{
			component = other.gameObject.AddComponent<Rigidbody>();
		}
		component.constraints = (RigidbodyConstraints)56;
		component.mass = 0.1f;
		component.isKinematic = false;
		if (other.CompareTag("Gem"))
		{
			other.tag = "GemCollected";
		}
		else
		{
			other.tag = "Player";
		}
		component.AddForce(new Vector3(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-2f, 0.5f), 0f), ForceMode.Impulse);
	}

	private void OnTriggerEnter(Collider other)
	{
		Booster component;
		if (other.CompareTag("Finish") || other.CompareTag("Gem"))
		{
			_colliders.Add(other);
		}
		else if (other.CompareTag("Booster") && other.TryGetComponent<Booster>(out component))
		{
			component.GetBooster();
		}
	}

	private void OnTriggerExit(Collider other)
	{
		if ((other.CompareTag("Finish") || other.CompareTag("Gem")) && _colliders.Contains(other))
		{
			_colliders.Remove(other);
		}
	}

	private void FixedUpdate()
	{
		for (int i = 0; i < _colliders.Count; i++)
		{
			if (UnityEngine.Random.Range(1, _randomRoll) == 1)
			{
				AddPhysics(_colliders[i]);
				if (!isOpponent)
				{
					progress.KillBlock();
				}
				else
				{
					progress.KillBlockOpponent();
				}
				gameplayAudio.PlaySound(GameplaySoundType.DigSound);
				gameplayHaptic.PlayHaptic(HapticType.Dig);
				_colliders.RemoveAt(i);
				i--;
			}
		}
	}

	public void Trigged(Collider other)
	{
	}

	public void SetKinematic(bool active)
	{
		for (int i = 0; i < _rigs.Length; i++)
		{
			_rigs[i].isKinematic = active;
		}
	}

	public void UpdateRotationSpeed()
	{
		rotationSpeed = 0.9f + (float)amount * 0.1f;
		if (rotationSpeed > 1.5f)
		{
			rotationSpeed = 1.5f;
		}
		if (TryGetComponent<MultipleSawsUpgrade>(out var component))
		{
			component.UpdateAnimationSpeed(rotationSpeed);
		}
		for (int i = 0; i < base.transform.childCount; i++)
		{
			base.transform.GetChild(i).TryGetComponent<Animator>(out var component2);
			if (component2 != null)
			{
				component2.SetFloat("speed", rotationSpeed);
				break;
			}
		}
	}
}
